package com.bkck.sharkblockade.levels.shark;

import com.bkck.sharkblockade.levels.ball.Ball;
import com.bkck.sharkblockade.levels.shark.Shark;
import android.graphics.Bitmap;
import android.graphics.Canvas;

/**
 * The eatBall class. Unsurprisingly, this handles the animation for when the shark eats the ball.
 * 
 * @author Team BKCK
 * 
 */

public class EatBall {

  // An array of bitmap images to draw for the animation
  private Bitmap[] images;

  // The coordinates of the animation
  private float x;
  private float y;

  // The dimensions of the animation
  private int width;
  //private int height;

  // Frame duration in milliseconds
  private int frameDuration = 100;

  // Variables needed to check the actual frame duration.
  private long lastUpdateTime = -1;
  private long elapsedTime = 0;

  // The current image
  private int currentImage;

  // Whether or not the shark is eating the ball
  private boolean eating;

  private Shark shark; 

  /**
   * The constructor.
   * 
   * @param images The array of bitmap images for the animation
   * 
   */

  public EatBall(Bitmap[] images) {
    this.images = images;
    this.width = images[0].getWidth();
    //this.height = images[0].getHeight();
    this.x = 0;
    this.y = 0;
    this.currentImage = 0;
    this.eating = false;
  }

  /**
   * Returns true if the shark is eating the ball.
   * 
   * @return eating A boolean true if the shark is eating the ball, false otherwise.
   */

  public boolean eating() {
    return (eating);
  }

  /**
   * Start the eating animation.
   * 
   * @param shark The Shark eating the ball.
   * 
   */

  public void startEating(Shark shark) {
    this.shark = shark;
    // +- 3 until other images are trimmed as well
    this.x = shark.getX() - (this.width - shark.getWidth() - 3);
    this.y = shark.getY() - 3;
    this.eating = true;
  }

  /**
   * Updates the animation.
   * 
   */

  public void update(Ball ball) {
    // Update only if the shark is eating
    if (this.eating == true) {
      // Update time variables
      long currentTime = System.currentTimeMillis();

      // If the animation is still ongoing
      // (at least, probably what this condition means;
      // still trying to understand Explosion.java)
      if (this.lastUpdateTime >= 0) {
        this.elapsedTime = this.elapsedTime + currentTime - this.lastUpdateTime;
        this.lastUpdateTime = currentTime;

        // If the elapsed time is greater than the frame duration,
        // then it is time to move to the next frame
        if (elapsedTime >= frameDuration) {
          // End once the animation is completed
          if (currentImage == 8) {
            if (shark.getColor() == Shark.BLUE) {
              shark.setXVelocity(-10);
            }
            if (shark.getColor() == Shark.ORANGE) {
              shark.setXVelocity(10);
            }
            endEating();
            ball.reset();            
          }

          // Otherwise update the images
          else {
            elapsedTime = 0;
            currentImage++;
          }
        }
      }
      else {
        this.lastUpdateTime = currentTime;
      }
    }
  }

  /**
   * End the eating animation.
   * 
   */

  public void endEating() {   
    this.currentImage = 0;
    this.eating = false;
    this.lastUpdateTime = -1;
  }

  /**
   * Draw the animation on the canvas.
   * 
   * @param canvas The canvas to draw on.
   * 
   */

  public void draw(Canvas canvas) {
    if (this.eating == true) {
      canvas.drawBitmap(this.images[this.currentImage], this.x, this.y, null);
    }
  }
  
  public boolean getEating() {
    return this.eating;
  }
}